package net.chasing.androidbaseconfig.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import net.chasing.androidbaseconfig.util.L;

public class NestedRecyclerView extends RecyclerView {
    public NestedRecyclerView(@NonNull Context context) {
        super(context);
    }

    public NestedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NestedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private float mDownX, mDownY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            mDownX = e.getRawX();
            mDownY = e.getRawY();
            getParent().requestDisallowInterceptTouchEvent(true);
        } else if (e.getAction() == MotionEvent.ACTION_MOVE) {
            boolean isDeal = false;
            int range;
            int offset;
            int extent;

            LayoutManager layoutManager = getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                boolean isVertical = layoutManager.canScrollVertically();
                if (isVertical) {
                    range = computeVerticalScrollRange();
                    offset = computeVerticalScrollOffset();
                    extent = computeVerticalScrollExtent();
                } else {
                    range = computeHorizontalScrollRange();
                    offset = computeHorizontalScrollOffset();
                    extent = computeHorizontalScrollExtent();
                }
                L.i("range: " + range + " offset:" + offset + " extent:" + extent);
                float x = e.getRawX();
                float y = e.getRawY();
                if (isVertical) {
                    if (offset == 0 && mDownY < y || offset + extent >= range && mDownY > y ||
                            Math.abs(mDownX - x) > Math.abs(mDownY - y)) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                        isDeal = true;
                    }
                } else {
                    if (offset == 0 && mDownX < x || offset + extent >= range && mDownX > x ||
                            Math.abs(mDownY - y) > Math.abs(mDownX - x)) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                        isDeal = true;
                    }
                }
            }

            if (!isDeal) {
                VelocityTracker mVelocityTracker = VelocityTracker.obtain();
                mVelocityTracker.addMovement(e);
                mVelocityTracker.computeCurrentVelocity(1000);

                if (Math.abs(mVelocityTracker.getXVelocity()) > Math.abs(mVelocityTracker.getYVelocity())) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                mVelocityTracker.recycle();
            }
        }
        return super.onInterceptTouchEvent(e);
    }
}
